import wx
import random
from card_manager_system import db_conn_util


class MainWindow(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title="lottery_wonderland", pos=(100, 100), size=(400, 300))
        # 创建面板
        self.pl = wx.Panel(self, pos=(200, 100), size=(400, 300))
        self.SetBackgroundColour((0, 0, 100))
        self.pl.SetBackgroundColour((0, 200, 0))
        # 创建静态文本
        self.staticText = wx.StaticText(self.pl, label='抽奖仙境', pos=(10, 50), size=(380, 50), style=wx.TE_CENTER)
        # 创建字体
        font = wx.Font(48, wx.FONTFAMILY_SWISS, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
        self.staticText.SetFont(font)
        # 创建按钮
        self.start_btn = wx.Button(self.pl, label='开始', pos=(100, 200), size=(100, 50))
        self.stop_btn = wx.Button(self.pl, label='停止', pos=(220, 200), size=(100, 50))
        self.start_btn.SetBackgroundColour((128, 128, 128))
        # 初始化抽奖名单
        self.lottery_name_list = []
        self.init_lottery_name_list()
        # 初始化定时器
        self.timer = None
        # 绑定事件
        self.start_btn.Bind(wx.EVT_BUTTON, self.start_lottery)
        self.stop_btn.Bind(wx.EVT_BUTTON, self.stop_lottery)

    def start_lottery(self,event):
        if self.timer is None or not self.timer.IsRunning():
            self.timer = wx.Timer(self)
            self.Bind(wx.EVT_TIMER, self.update_time, self.timer)
            self.timer.Start(200)

    def stop_lottery(self,event):
        if self.timer is not None and self.timer.IsRunning():
            self.timer.Stop()

    def update_time(self,event):
        self.staticText.SetLabel(random.choice(self.lottery_name_list))

    def init_lottery_name_list(self):
        cards = db_conn_util.show_card()
        if not cards:
            return
        for card in cards:
            self.lottery_name_list.append(card.name)


if __name__ == '__main__':
    app = wx.App()
    # 创建窗口
    frm = MainWindow()
    # 显示窗口
    frm.Show()
    app.MainLoop()
